package com.fr.data.impl;

import com.fr.base.XMLable;
import com.fr.base.core.BaseCoreUtils;
import com.fr.base.core.ComparatorUtils;
import com.fr.base.core.FRCoreContext;
import com.fr.base.core.RSADecode;
import com.fr.base.core.json.JSONObject;
import com.fr.base.core.util.vt.VT4FR;
import com.fr.base.core.util.vt.VT4FR.FUNC;
import com.fr.base.xml.XMLPrintWriter;
import com.fr.base.xml.XMLReadable;
import com.fr.base.xml.XMLableReader;
import com.fr.data.AbstractTableData;
import com.fr.data.TableDataException;
import com.fr.data.core.DataXMLUtils;
import com.fr.report.core.ParameterHelper;
import com.fr.report.core.cal.SheetExecuter;
import com.fr.report.core.cal.SynchronizedSEList;
import com.fr.report.io.xml.ReportXMLUtils;
import com.fr.report.parameter.Parameter;
import com.fr.util.Consts;
import com.fr.util.Utils;
import java.io.ByteArrayOutputStream;
import java.util.Calendar;
import java.util.List;

public class DBTableData extends AbstractTableData
  implements XMLable
{
  private static boolean shareAvailable = true;
  private DatabaseConnection database;
  private String query;
  private int resultIndexOfOut;
  private boolean share;
  private transient TableDataAdapter adapter;
  private int maxMemRowCount;
  private String pageQuerySql;

  public int getResultIndexOfOut()
  {
    return this.resultIndexOfOut;
  }

  public void setResultIndexOfOut(int paramInt)
  {
    this.resultIndexOfOut = paramInt;
  }

  public DBTableData()
  {
    this("sun.jdbc.odbc.JdbcOdbcDriver", "", "", "", "");
  }

  public DBTableData(String paramString1, String paramString2, String paramString3, String paramString4, String paramString5)
  {
    this(new JDBCDatabaseConnection(paramString1, paramString2, paramString3, paramString4), paramString5);
  }

  public DBTableData(DatabaseConnection paramDatabaseConnection, String paramString)
  {
    this.share = false;
    this.maxMemRowCount = -1;
    this.pageQuerySql = null;
    setDatabase(paramDatabaseConnection);
    setQuery(paramString);
  }

  public DatabaseConnection getDatabase()
  {
    return this.database;
  }

  public void setDatabase(DatabaseConnection paramDatabaseConnection)
  {
    this.database = paramDatabaseConnection;
  }

  public String getQuery()
  {
    return this.query;
  }

  public String getPageQuerySql()
  {
    return this.pageQuerySql;
  }

  public void setPageQuerySql(String paramString)
  {
    this.pageQuerySql = paramString;
  }

  public int getMaxMemRowCount()
  {
    return this.maxMemRowCount;
  }

  public void setMaxMemRowCount(int paramInt)
  {
    this.maxMemRowCount = paramInt;
  }

  public boolean isShare()
  {
    return this.share;
  }

  public void setShare(boolean paramBoolean)
  {
    this.share = paramBoolean;
  }

  public boolean isShareTableData()
  {
    return ((shareAvailable) && (this.share));
  }

  public void setQuery(String paramString)
  {
    this.query = paramString;
  }

  private TableDataAdapter createAdapter()
  {
    if (this.adapter == null)
    {
      String str = getNewQuery();
      SheetExecuter localSheetExecuter = SynchronizedSEList.getSheetExecuter(Thread.currentThread());
      if (localSheetExecuter != null)
        this.adapter = localSheetExecuter.createDBTableDataAdapter(this, str);
      else
        this.adapter = SheetExecuter.createDefaultDBTableDataAdapter(this, str);
    }
    return this.adapter;
  }

  public static TableDataAdapter createCacheableAdapter(DatabaseConnection paramDatabaseConnection, String paramString, int paramInt, long paramLong)
  {
    Object localObject = new DiskCacheAdapter(paramDatabaseConnection, paramString, paramInt);
    if ((paramLong < 0L) || (((TableDataAdapter)localObject).shortcutGetRowCount() < paramLong))
      localObject = new MemCachedAdapter(paramDatabaseConnection, paramString, paramInt);
    return ((TableDataAdapter)localObject);
  }

  protected String getNewQuery()
  {
    String str = ParameterHelper.analyzeCurrentContextTableData4Templatee(getQuery(), this.parameters);
    return str;
  }

  public int getColumnCount()
    throws TableDataException
  {
    return createAdapter().getColumnCount();
  }

  public String getColumnName(int paramInt)
    throws TableDataException
  {
    String str = createAdapter().getColumnName(paramInt);
    return str;
  }

  public boolean hasRow(int paramInt)
    throws TableDataException
  {
    return createAdapter().hasRow(paramInt);
  }

  public int getRowCount()
    throws TableDataException
  {
    return createAdapter().getRowCount();
  }

  public Object getValueAt(int paramInt1, int paramInt2)
    throws TableDataException
  {
    return createAdapter().getValueAt(paramInt1, paramInt2);
  }

  public void setParameters(Parameter[] paramArrayOfParameter)
  {
    this.parameters = paramArrayOfParameter;
  }

  public void readXML(XMLableReader paramXMLableReader)
  {
    super.readXML(paramXMLableReader);
    if (paramXMLableReader.isChildNode())
    {
      String str2;
      String str1 = paramXMLableReader.getTagName();
      if ("Attributes".equals(str1))
      {
        if ((str2 = paramXMLableReader.getAttr("share")) != null)
          setShare(Boolean.valueOf(str2).booleanValue());
        if ((str2 = paramXMLableReader.getAttr("maxMemRowCount")) != null)
          setMaxMemRowCount(Integer.parseInt(str2));
        if ((str2 = paramXMLableReader.getAttr("indexOfO")) != null)
          setResultIndexOfOut(Integer.parseInt(str2));
      }
      else
      {
        Object localObject1;
        if (("Connection".equals(str1)) || ("Database".equals(str1)))
        {
          if (paramXMLableReader.getAttr("class") != null)
          {
            localObject1 = DataXMLUtils.readXMLConnection(paramXMLableReader);
            if (localObject1 instanceof DatabaseConnection)
              setDatabase((DatabaseConnection)localObject1);
          }
          else
          {
            localObject1 = new JDBCDatabaseConnection();
            setDatabase((DatabaseConnection)localObject1);
            if ((str2 = paramXMLableReader.getAttr("driver")) != null)
              ((JDBCDatabaseConnection)localObject1).setDriver(str2);
            if ((str2 = paramXMLableReader.getAttr("url")) != null)
              ((JDBCDatabaseConnection)localObject1).setURL(str2);
            if ((str2 = paramXMLableReader.getAttr("user")) != null)
              ((JDBCDatabaseConnection)localObject1).setUser(str2);
            if ((str2 = paramXMLableReader.getAttr("password")) != null)
              ((JDBCDatabaseConnection)localObject1).setPassword(ReportXMLUtils.unPasswordString(str2));
          }
        }
        else
        {
          if ("Query".equals(str1))
          {
            if ((str2 = paramXMLableReader.getElementValue()) != null)
              setQuery(str2);
            localObject1 = getParameters();
            if ((localObject1 == null) || (localObject1.length <= 0))
              return;
            int i = 0;
            while (true)
            {
              if (i >= localObject1.length)
                return;
              Object localObject2 = localObject1[i];
              this.query = Utils.replaceAllString(this.query, "[?" + localObject2.getName() + "?]", "[?" + localObject2.getName() + "|" + localObject2.getValue() + "?]");
              ++i;
            }
          }
          if ("PageQuery".equals(str1))
          {
            if ((str2 = paramXMLableReader.getElementValue()) != null)
              setPageQuerySql(str2);
          }
          else if ("JDBCDatabase".equals(str1))
          {
            localObject1 = new JDBCDatabaseConnection();
            paramXMLableReader.readXMLObject((XMLReadable)localObject1);
            setDatabase((DatabaseConnection)localObject1);
          }
        }
      }
    }
  }

  public void writeXML(XMLPrintWriter paramXMLPrintWriter)
  {
    super.writeXML(paramXMLPrintWriter);
    paramXMLPrintWriter.startTAG("Attributes");
    if (this.share)
      paramXMLPrintWriter.attr("share", this.share);
    if (this.resultIndexOfOut > 0)
      paramXMLPrintWriter.attr("indexOfO", this.resultIndexOfOut);
    paramXMLPrintWriter.attr("maxMemRowCount", getMaxMemRowCount()).end();
    if (this.database != null)
      DataXMLUtils.writeXMLConnection(paramXMLPrintWriter, this.database);
    paramXMLPrintWriter.startTAG("Query").textNode(getQuery()).end();
    if (this.pageQuerySql != null)
      paramXMLPrintWriter.startTAG("PageQuery").textNode(this.pageQuerySql).end();
  }

  public void release()
    throws Exception
  {
    if (this.adapter != null)
    {
      this.adapter.unRegister(this);
      this.adapter.tryDestroy();
      this.adapter = null;
    }
  }

  public String toString()
  {
    return this.database + "[Query:" + getQuery() + "]";
  }

  public Object clone()
    throws CloneNotSupportedException
  {
    DBTableData localDBTableData = new DBTableData();
    localDBTableData.setDatabase(getDatabase());
    localDBTableData.parameters = ParameterHelper.cloneParameters(getParameters());
    localDBTableData.setQuery(getQuery());
    localDBTableData.setMaxMemRowCount(getMaxMemRowCount());
    localDBTableData.setPageQuerySql(getPageQuerySql());
    localDBTableData.setResultIndexOfOut(getResultIndexOfOut());
    return localDBTableData;
  }

  public void finalize()
    throws Throwable
  {
    finalize();
    release();
  }

  public boolean equals(Object paramObject)
  {
    return ((paramObject instanceof DBTableData) && (super.equals(paramObject)) && (ComparatorUtils.equals(this.query, ((DBTableData)paramObject).query)) && (ComparatorUtils.equals(this.database, ((DBTableData)paramObject).database)) && (this.maxMemRowCount == ((DBTableData)paramObject).maxMemRowCount) && (ComparatorUtils.equals(this.pageQuerySql, ((DBTableData)paramObject).pageQuerySql)));
  }

  static
  {
    byte[] arrayOfByte = FRCoreContext.getBytes();
    ByteArrayOutputStream localByteArrayOutputStream = new ByteArrayOutputStream();
    try
    {
      RSADecode.decode(arrayOfByte, localByteArrayOutputStream);
    }
    catch (Exception localException1)
    {
    }
    try
    {
      JSONObject localJSONObject = new JSONObject(new String(localByteArrayOutputStream.toByteArray(), "UTF-8"));
      shareAvailable = (localJSONObject.getLong("deadline") > Calendar.getInstance().getTimeInMillis()) && (localJSONObject.has("version")) && (ComparatorUtils.equals(Consts.VERSION, localJSONObject.getString("version"))) && (((localJSONObject.getLong("deadline") - Calendar.getInstance().getTimeInMillis() < 3153600000L) || ((localJSONObject.has("macaddress")) && (BaseCoreUtils.getMacAddresses().contains(localJSONObject.getString("macaddress")))))) && (VT4FR.PERFORMCE_OPTIMISE.support());
    }
    catch (Exception localException2)
    {
    }
  }
}